import {createRouter, createWebHistory} from 'vue-router'
import {useAdminStore} from '@/stores/modules/user/index.js'
// 后台路由
const Login = () => import('@/views/admin/Login/login.vue')
const NotFound = () => import('@/views/error/404.vue')
const Admin = () => import('@/views/admin/layout/home.vue')
const Shopstatistics = () => import('@/views/admin/statistics/shopstatistics.vue')
const Petfiles = () => import('@/views/admin/user/petmessage.vue')
const Customfiles = () => import('@/views/admin/user/customfiles.vue')
const Foster = () => import('@/views/admin/pet/fosterPet.vue')
const Sell = () => import('@/views/admin/pet/sellPet.vue')
const Shopgoods = () => import('@/views/admin/shop/shopgoods.vue')
const Adminorder = () => import('@/views/admin/shop/orderview.vue')
const Customcharge = () => import('@/views/admin/shop/customcharge.vue')
const Admincharge = () => import('@/views/admin/user/admincharge.vue')
// 前台路由
const Customlogin = () => import('@/views/shop/login/login.vue')
const Netshop = () => import('@/views/shop/layout/Home.vue')
const Petdetail = () => import('@/views/shop/petdetail.vue')
const Shopdetail = () => import('@/views/shop/shopdetail.vue')
const Comfirm = () => import('@/components/netshop/comfirm.vue')
const Settlement = () => import('@/components/netshop/settlement.vue')
const Customcenter = () => import('@/views/shop/customcenter.vue')
const MineInfo = () => import('@/views/shop/mineInfo.vue')
const UserinfoEdit = () => import('@/views/shop/userInfoEdit.vue')
const Orderview = () => import('@/views/shop/orderview.vue')
const Security = () => import('@/views/shop/security.vue')
const Address = () => import('@/views/shop/address.vue')
const Establish = () => import('@/views/shop/establish.vue')
const Reservation = () => import('@/views/shop/reservation.vue')

const useAdminrouter = createRouter({
    // 后台路由
    history: createWebHistory(import.meta.env.BASE_URL),
    mode: 'history',
    routes: [
        {path: '/:pathMatch(.*)*', component: NotFound},
        {path: '/login', component: Login},  // 登录页
        {
            path: '/admin', component: Admin, redirect: '/admin/shopstatistics',
            children: [
                {path: 'shopstatistics', component: Shopstatistics},
                {path: 'petfiles', component: Petfiles},
                {path: 'customfiles', component: Customfiles},
                {path: 'foster', component: Foster},
                {path: 'sell', component: Sell},
                {path: 'shopgoods', component: Shopgoods},
                {path: 'adminorder', component: Adminorder},
                {path: 'customcharge', component: Customcharge},
                {path: 'admincharge', component: Admincharge},
            ]
        },
        // 前台路由
        {
            path: '/',
            redirect: '/netshop'
        },
        {
            path: '/customlogin',
            component: Customlogin
        },
        {
            path: "/netshop",
            component: Netshop,
        },
        {
            path: '/petdetail/:id?',
            name: 'petdetail',
            component: Petdetail,
            props: true
        },
        {
            path: '/shopdetail/:id?',
            name: 'shopdetail',
            component: Shopdetail,
            props: true
        },
        {
            path: '/comfirm',
            name: 'comfirm',
            component: Comfirm,
            props: true
        },
        {
            path: '/settlement',
            name: 'settlement',
            component: Settlement,
        },
        {
            path: '/customcenter',
            component: Customcenter,
            children: [
                {
                    path: '/mineInfo',
                    name: 'mineInfo',
                    component: MineInfo
                },
                {
                    path: '/userinfoEdit',
                    name: 'userinfoEdit',
                    component: UserinfoEdit
                },
                {
                    path: '/orderview',
                    name: 'orderview',
                    component: Orderview
                },
                {
                    path: '/security',
                    name: 'security',
                    component: Security
                },
                {
                    path: '/address',
                    name: 'address',
                    component: Address
                },
                {
                    path: '/establish',
                    name: 'establish',
                    component: Establish
                },
                {
                    path: '/reservation',
                    name: 'reservation',
                    component: Reservation
                },
            ]
        }


    ]
})

// 路由守卫
useAdminrouter.beforeEach((to, from, next) => {
    if (to.path === '/netshop') {
        next()
    } else if (to.path.startsWith('/admin')) {
        const token = localStorage.getItem('admin_token')
        if (!token) {
            next('/login')
        } else {
            next()
        }
    } else {
        next()
    }
})

export default useAdminrouter
